package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartServiceImpl implements CartService {
    private Connection con = null;
    private int isRight = 0;
    @Override
    public List<Cart> slistCart(String userid, Integer businessid) {
        List<Cart> list = new ArrayList<>();
        CartDao dao = new CartDaoImpl();
        try {
            list = dao.dlistCart(userid,businessid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int ssaveCart(Cart cart) {

        CartDao dao = new CartDaoImpl();
        try {
            DBUtil.getConnection();
            DBUtil.beginTransaction();
            isRight = dao.dsaveCart(cart);
            DBUtil.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }

    @Override
    public int supdateCart(Cart cart) {
        DBUtil.getConnection();
        CartDao dao = new CartDaoImpl();
        try {
            isRight = dao.dupdateCart(cart);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public int sremoveCart(Cart cart) {

        CartDao dao = new CartDaoImpl();
        try {
            DBUtil.beginTransaction();
            isRight = dao.dremoveCart(cart);
            DBUtil.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();



        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }
}
